package filter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import error.GedivaException;

/**
 * filters out values less than or greater than two user-inputted values
 * 
 * @author Trevor
 * 
 */
public abstract class TwoValueFilter extends Filter<Double, List<Double>>
{
    private String myValueFiltered;
    private double minValue; // minimum value left in the list
    private double maxValue; // maximum value left in the list

    /**
     * constructor, given a list of constraints
     * 
     * @param constraints
     */
    protected TwoValueFilter(String termToBeFiltered)
    {
        super("ValueFilter");
        myValueFiltered = termToBeFiltered;
    }

    /**
     * removes any items with x values outside of the given bounds
     */
    public Map<Object, List<Object>> filterData(Map<Object, List<Object>> data)
    {
        try
        {
            Map<Object, List<Object>> outputData = new HashMap<Object, List<Object>>(
                    data);
            double firstValue = Double.parseDouble(super.getStoredConstraints()
                    .get(0));
            double secondValue = Double.parseDouble(super
                    .getStoredConstraints().get(1));
            minValue = Math.min(firstValue, secondValue);
            maxValue = Math.max(firstValue, secondValue);

            for (Object l : data.keySet())
            {
                if (myValueFiltered.compareTo("x") == 0)
                {
                    if ((Double) l > maxValue || (Double) l < minValue)
                        outputData.remove(l);
                }
                else
                {
                    if ((Double) outputData.get(l).get(0) > maxValue
                            || (Double) outputData.get(l).get(0) < minValue)
                        outputData.remove(l);
                }
            }
            return outputData;
        }
        catch (Exception e)
        {
            throw new GedivaException("Improper input");
        }
    }
}
